/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.cncqs.backend.applet.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.common.MyResponseBody;
import com.jeesite.modules.sys.entity.DictData;
import com.jeesite.modules.sys.service.DictDataService;
import com.jeesite.modules.utils.ExcelUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.cncqs.backend.applet.entity.AppletFoods;
import com.cncqs.backend.applet.service.AppletFoodsService;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;

/**
 * 食品表Controller
 * @author cxy
 * @version 2019-05-05
 */
@Controller
@RequestMapping(value = "${adminPath}/applet/appletFoods")
public class AppletFoodsController extends BaseController {

	private final String[] titles = {"成品名称","适合餐次","所属分类","销售价格","成品说明","标签","标签2"};

	@Autowired
	private AppletFoodsService appletFoodsService;
	@Autowired
	private DictDataService dictDataService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public AppletFoods get(Long id, boolean isNewRecord) {
		return appletFoodsService.get(id == null ? "" : id.toString(), isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("applet:appletFoods:view")
	@RequestMapping(value = {"list", ""})
	public String list(AppletFoods appletFoods, Model model) {
		model.addAttribute("appletFoods", appletFoods);
		return "backend/applet/appletFoodsList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("applet:appletFoods:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<AppletFoods> listData(AppletFoods appletFoods, HttpServletRequest request, HttpServletResponse response) {
		appletFoods.setPage(new Page<>(request, response));
		Page<AppletFoods> page = appletFoodsService.findPage(appletFoods);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("applet:appletFoods:view")
	@RequestMapping(value = "form")
	public String form(AppletFoods appletFoods, Model model) {
		model.addAttribute("appletFoods", appletFoods);
		return "backend/applet/appletFoodsForm";
	}

	/**
	 * 保存食品表
	 */
	@RequiresPermissions("applet:appletFoods:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated AppletFoods appletFoods) {
		appletFoodsService.save(appletFoods);
		return renderResult(Global.TRUE, text("保存食品表成功！"));
	}
	
	/**
	 * 删除食品表
	 */
	@RequiresPermissions("applet:appletFoods:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(AppletFoods appletFoods) {
		appletFoodsService.delete(appletFoods);
		return renderResult(Global.TRUE, text("删除食品表成功！"));
	}
	/**
	 * Excel上传菜品
	 * */
	@RequestMapping("upload")
	@RequiresPermissions("applet:appletFoods:edit")
	@ResponseBody
	public String upload(MultipartFile file) throws Exception {
		List<List<String>> excelSheet = ExcelUtils.getExcelSheet(file,null);
		for (int i = 0; i < excelSheet.size(); i++) {
			List<String> strings = excelSheet.get(i);
			AppletFoods appletFoods = new AppletFoods();
			appletFoods.setName(strings.get(0));
			DictData dictData = new DictData();
			dictData.setDictType("com_foods_type");
			List<DictData> list = dictDataService.findList(dictData);
			for (DictData data : list) {
				if (data.getTreeNames().equals(strings.get(1))) {
					appletFoods.setType(Integer.parseInt(data.getDictValue()));
				}
			}
			dictData.setDictType("com_foods_category");
			List<DictData> categoryList = dictDataService.findList(dictData);
			for (DictData data : categoryList) {
				if (data.getTreeNames().equals(strings.get(2))) {
					appletFoods.setCategory(Integer.parseInt(data.getDictValue()));
				}
			}
			appletFoods.setPrice(new BigDecimal(strings.get(3)));
			appletFoods.setRemarks(strings.get(4));

			appletFoodsService.insert(appletFoods);
		}

		return renderResult(Global.TRUE, text("上传成功！"));
	}
	
}